package decision_tree.dao;

import java.util.Arrays;
import java.util.List;

import decision_tree.question.NgramQuestion;
import decision_tree.question.Question;

public class QuestionIO {
	public Question parse(String line) {
		String [] parts = line.split(" :  ");
		String type = parts[0].trim();
		String conf = parts[1].trim();
		
		Question result;
		if(type.compareTo("ngram") == 0) {
			result = getNgramQuestion(conf);
		} else {
			throw new RuntimeException();
		}
		
		return result;
	}
	
	public String write(Question question) {
		return question.toString();
	}
	
	protected Question getNgramQuestion(String conf) {
		String [] terms = conf.replace("[", "").replace("]", "").replace(" ", "").split(",");
		List<String> pattern = Arrays.asList(terms);
		
		Question result = new NgramQuestion();
		result.initialize(pattern);
		return result;
	}
}
